package com.crack.nga2.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.crack.nga2.R;
import com.crack.nga2.activity.userCenter.UserReplyActivity;
import com.crack.nga2.activity.userCenter.UserThemeActivity;
import com.crack.nga2.entity.User;
import com.crack.nga2.volley.PhotoUrlRequest;

public class OtherUserActivity extends BaseActivity {
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_user);
        final ImageView photo = (ImageView) findViewById(R.id.iv_photo);
        user = (User) getIntent().getSerializableExtra("user");
        TextView name = (TextView) findViewById(R.id.tv_name);
        TextView group = (TextView) findViewById(R.id.tv_group);
        name.setText(user.getUsername());
        group.setText(user.getF_level());
        String url = "http://bbs.ngacn.cc/nuke.php?__act=get_avatar&__lib=ucp&uid=" + user.getUid() + "&__nodb&lite=js";
        RequestQueue queue = NgaApplication.getQueue();
        queue.add(new PhotoUrlRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loadPhoto(photo, response);
            }
        }));
    }

    private void loadPhoto(final ImageView photo, String url) {
        ImageLoader imageLoader = NgaApplication.getImageLoader();
        imageLoader.get(url, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                Bitmap bitmap = response.getBitmap();
                if (bitmap != null) {
                    photo.setImageBitmap(bitmap);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
    }

    public void doClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.action_return:
                finishForAnimation();
                break;
            case R.id.im:
                intent = new Intent(this, PostMessageActivity.class);
                intent.putExtra("uid", user.getUid());
                startActivity(intent);
                break;
            case R.id.message:
                intent = new Intent(this, PostMessageActivity.class);
                intent.putExtra("uid", user.getUid());
                startActivity(intent);
                break;
            case R.id.other_reply:
                intent = new Intent(this, UserReplyActivity.class);
                intent.putExtra("uid", user.getUid());
                startActivity(intent);
                break;
            case R.id.other_themes:
                intent = new Intent(this, UserThemeActivity.class);
                intent.putExtra("uid", user.getUid());
                startActivity(intent);
                break;
        }
    }
}
